﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EcologyFuture.DataCollection
{
  /// <summary>
  /// The <c>WeatherInfo</c> struct represents data format for weather that collects from different sources.
  /// </summary>
  struct WeatherInfo
  {
    /// <summary>
    /// Date and time of weather state in UTC.
    /// </summary>
    public DateTime date;
    /// <summary>
    /// String represantation of country (may be different for different sources).
    /// </summary>
    public string country;
    /// <summary>
    /// String represantation of city (may be different for different sources).
    /// </summary>
    public string city;
    /// <summary>
    /// Wind direction in degrees.
    /// </summary>
    public int direction;
    /// <summary>
    /// Wind speed in metres per second.
    /// </summary>
    public float speed;
    /// <summary>
    /// Humidity in percents.
    /// </summary>
    public int humidity;
    /// <summary>
    /// Pressure in mm Hg (Torr).
    /// </summary>
    public float pressure;
    /// <summary>
    /// Temperature in Celsius degrees.
    /// </summary>
    public int temperature;
    /// <summary>
    /// Class name for data collector (for internal use).
    /// </summary>
    public string dataCollectorName;
  }
  /// <summary>
  /// Interface that all data collectors must implement.
  /// </summary>
  interface IDataCollector
  {
    /// <summary>
    /// Method that all data collectors must implement. Top-level method for
    /// data collectors.
    /// </summary>
    /// <returns>Weather information in <c>WeatherInfo</c> struct.</returns>
    /// <remarks>Data collector doesn't use <c>dataCollectorName</c> field.</remarks>
    WeatherInfo CollectData();
  }
  /// <summary>
  /// The <c>DataCollectionKernel</c> class is the main class in <c>DataCollection</c> module. It collects data from
  /// all sources.
  /// </summary>
  class DataCollectionKernel
  {
    /// <summary>
    /// List of all data collectors.
    /// </summary>
    private List<IDataCollector> dataCollectors;
    /// <summary>
    /// List of data collectors names (for internal use).
    /// </summary>
    private List<string> dataCollectorNames;
    /// <summary>
    /// Initializes a new instance of <c>DataCollectionKernel</c> class (adds all data collectors).
    /// </summary>
    public DataCollectionKernel()
    {
      dataCollectors = new List<IDataCollector>();
      dataCollectorNames = new List<string>();
      AddDataCollector(new YahooWeatherDataCollector());
      AddDataCollector(new WeatherCoUaDataCollector());
    }
    /// <summary>
    /// Adds a data collector to list of data collectors.
    /// </summary>
    /// <param name="dataCollector">Class object that implements <c>IDataCollector</c> interface.</param>
    private void AddDataCollector(IDataCollector dataCollector)
    {
      dataCollectors.Add(dataCollector);
      string dataCollectorName = dataCollector.GetType().Name;
      dataCollectorNames.Add(dataCollectorName);
    }
    /// <summary>
    /// Collects all data from all data sources.
    /// </summary>
    /// <returns>List of weather information from all data sources.</returns>
    public List<WeatherInfo> CollectData()
    {
      List<WeatherInfo> data = new List<WeatherInfo>(dataCollectors.Count);
      List<string>.Enumerator enumer = dataCollectorNames.GetEnumerator();
      foreach (IDataCollector dataCollector in dataCollectors)
      {
        enumer.MoveNext();
        try
        {
          WeatherInfo weatherInfo = dataCollector.CollectData();
          weatherInfo.dataCollectorName = enumer.Current;
          data.Add(weatherInfo);
        }
        catch (Helper.EFExeption e)
        {
          Console.WriteLine(e.Message);
        }
      }
      return data;
    }
  }
}
